package com.jie.myprogress.localS;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.jie.myprogress.R;
import com.jie.myprogress.jie.intel.ProcessMulti;
import com.jie.myprogress.remoteS.RemoteService;

/**
 * Created by ${张英杰} on 2016/9/19.
 */
public class LocalService extends Service {

    private MyBinder binder;
    private MyServiceConnection coon;

    @Override
    public void onCreate() {
        super.onCreate();
        if (binder == null) {
            binder = new MyBinder();
        }
        coon = new MyServiceConnection();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       //绑定远程服务
        this.bindService(new Intent(this, RemoteService.class),coon, Context.BIND_IMPORTANT);
//        //远程服务已开启，连接本地服务
//        //Notification 通知提高优先级
//        Notification.Builder builder = new Notification.Builder(this);
//        builder.setSmallIcon(R.mipmap.ic_launcher);//图表
//        builder.setTicker("显示第二个通知");
//        builder.setContentTitle("本地通知");//标题
//        builder.setContentText("点击查看详细内容");//消息内容
//        builder.setWhen(System.currentTimeMillis());//发送时间
//        builder.setDefaults(Notification.DEFAULT_ALL);//设置默认提示音，震动方式，灯光
//        builder.setAutoCancel(true);//打开程序后图标消失
////        Intent intent =new Intent (MainActivity.this,Center.class);
//        Notification notification = new Notification(R.mipmap.ic_launcher, "远端服务启动中", System.currentTimeMillis());
//        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
//        builder.setContentIntent(pendingIntent);
//        Notification n1 = builder.build();
//        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        manager.notify(124, n1); // 通过通知管理器发送通知
//
//        //让该service前台运行，避免手机休眠时系统自动抹杀服务。
//        //如果id为0，那么状态栏的notification将不会显示。
//        startForeground(startId, notification);
        return START_STICKY;
    }

    class MyBinder extends ProcessMulti.Stub {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public String aidlGetProcessName() throws RemoteException {
            return "Local Service_本地服务";
        }
    }

    class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //链接成功
            Log.i("INFO===","远程服务 链接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //链接异常
            Toast.makeText(LocalService.this,"远程服务被干掉",Toast.LENGTH_SHORT).show();
            //启动远程服务
            LocalService.this.startService(new Intent(LocalService.this,RemoteService.class));
            //主动链接远程服务
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class),coon, Context.BIND_IMPORTANT);
        }
    }
}
